﻿using System;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument
{
    /// <summary>
    /// ManagerProject 的摘要说明。
    /// </summary>
    public partial class ManagerProjectPage : Page
    {
        public static string ClassID = "";
        public string Action = "";

        private void Page_Load(object sender, EventArgs e)
        {
            ClassID = (Request.QueryString["ClassID"] != null) ? Request.QueryString["ClassID"] : "";
            Action = (Request.QueryString["Action"] != null) ? Request.QueryString["Action"] : "";

            if (!Page.IsPostBack)
            {
                PopulateRadioList();
                if (Action == "1")
                {
                    lblCreate.BackColor = Color.FromName("#1ED2CA");
                    btnRevise.Visible = false;
                    btnDelete.Visible = false;
                }
                if (Action == "2")
                {
                    var prj = new ProjectClass();
                    lblDelete.BackColor = Color.FromName("#1ED2CA");
                    btnRevise.Visible = false;
                    btnDelete.Visible = true;
                    btnSubmit.Visible = false;
                    PopulateReviseData();
                    if (ClassID != "1")
                    {
                        if (prj.IsExistSubClass(Int32.Parse(ClassID)))
                            btnDelete.Attributes["onClick"] = "javascript:alert('此项目还存在子节点，不能删除!!');return false;";
                        else
                            btnDelete.Attributes["onClick"] = "javascript:return confirm('您确认吗?');";
                    }
                    else
                    {
                        btnDelete.Attributes["onClick"] = "javascript:alert('根节点不能被删除!');return false;";
                    }
                }
                if (Action == "3")
                {
                    lblRevise.BackColor = Color.FromName("#1ED2CA");
                    btnSubmit.Visible = false;
                    btnDelete.Visible = false;
                    PopulateReviseData();
                }
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            AddProject();
        }

        private void btnRevise_Click(object sender, EventArgs e)
        {
            ReviseProject();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DeleteProject();
        }

        #region 添加项目

        /// <summary>
        /// 添加项目
        /// </summary>
        private void AddProject()
        {
            var prj = new ProjectClass();
            String Username = SiteContext.Current.UserName;
            if (DateTime.Parse(txtStartDate.Text) > DateTime.Parse(txtEndDate.Text))
            {
                Response.Write("<script language=javascript>alert('开始时间不能大于结束时间!');</script>");
            }
            else
            {
                try
                {
                    prj.Add(Int32.Parse(ClassID), txtClassName.Value, txtBrief.Value,
                        Username, Int32.Parse(Status.SelectedIndex.ToString()), Int32.Parse(txtScale.Value),
                        DateTime.Parse(txtStartDate.Text), DateTime.Parse(txtEndDate.Text));

                    Response.Write("<script language=javascript>alert('添加成功!');parent.LeftFrame.location='ProjectTreeView.aspx?classID=" + ClassID + "';</script>");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }

        #endregion

        #region 修改项目

        /// <summary>
        /// 修改项目
        /// </summary>
        private void ReviseProject()
        {
            var prj = new ProjectClass();
            String Username = SiteContext.Current.UserName;
            if (DateTime.Parse(txtStartDate.Text) > DateTime.Parse(txtEndDate.Text))
            {
                Response.Write("<script language=javascript>alert('开始时间不能大于结束时间!');</script>");
            }
            else
            {
                try
                {
                    prj.Revise(Int32.Parse(ClassID), txtClassName.Value, txtBrief.Value,
                        Int32.Parse(Status.SelectedIndex.ToString()), Int32.Parse(txtScale.Value),
                        DateTime.Parse(txtStartDate.Text), DateTime.Parse(txtEndDate.Text));

                    Response.Write("<script language=javascript>alert('修改成功!');parent.LeftFrame.location='ProjectTreeView.aspx?classID=" + ClassID + "';</script>");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }

        #endregion

        #region 删除项目

        /// <summary>
        /// 删除项目
        /// </summary>
        private void DeleteProject()
        {
            var prj = new ProjectClass();
            HttpCookie UserCookie = Request.Cookies["Username"];
            String Username = UserCookie.Value;
            try
            {
                prj.Delete(Int32.Parse(ClassID));
                Response.Write("<script language=javascript>alert('删除成功!');parent.location.reload();</script>");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
        }

        #endregion

        #region 绑定修改时数据

        /// <summary>
        /// 绑定修改时数据
        /// </summary>
        private void PopulateReviseData()
        {
            var prj = new ProjectClass();
            try
            {
                using (var dataReader = prj.GetProjectDetail(Int32.Parse(ClassID)))
                {
                    dataReader.Read();
                    txtClassName.Value = dataReader[0].ToString();
                    txtBrief.Value = dataReader[7].ToString();
                    txtScale.Value = dataReader[6].ToString();
                    txtStartDate.Text = ProjectClass.changeString(dataReader[3].ToString());
                    txtEndDate.Text = ProjectClass.changeString(dataReader[4].ToString());
                    Status.SelectedIndex = Int32.Parse(dataReader[5].ToString());
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
        }

        #endregion

        #region 初始化单选框

        /// <summary>
        /// 对单选框进行初始化
        /// </summary>
        private void PopulateRadioList()
        {
            Status.Items.Add(new ListItem("运行中", "1"));
            Status.Items.Add(new ListItem("等待运行", "2"));
            Status.Items.Add(new ListItem("挂起", "3"));
            Status.Items.Add(new ListItem("取消", "4"));
            Status.Items.Add(new ListItem("完成", "5"));
            Status.SelectedIndex = 0;
        }

        #endregion

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
            this.btnRevise.Click += new System.EventHandler(this.btnRevise_Click);
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}